-
-
Notifications
You must be signed in to change notification settings - Fork 9k
修复 RsaCryptoUtil 无法加密继承字段和嵌套对象的问题 #3841
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Conversation
Co-authored-by: binarywang <[email protected]>
Co-authored-by: binarywang <[email protected]>
Co-authored-by: binarywang <[email protected]>
Co-authored-by: binarywang <[email protected]>
Co-authored-by: binarywang <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
此 PR 修复了 RsaCryptoUtil.encryptFields() 方法无法加密父类继承字段和嵌套对象字段的问题。原有实现使用 getDeclaredFields() 只能获取当前类声明的字段,导致父类字段和集合元素内部的加密字段被忽略。
主要变更:
- 新增
getAllFields()辅助方法,递归获取类及其所有父类的字段 - 修改
encryptField()方法,使用getAllFields()替代getDeclaredFields() - 新增测试类
RsaCryptoUtilTest,包含嵌套对象、继承场景和反射验证的测试用例
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 9 comments.
| File | Description |
|---|---|
| weixin-java-pay/src/main/java/com/github/binarywang/wxpay/v3/util/RsaCryptoUtil.java | 新增 getAllFields() 方法支持获取继承字段,修改 encryptField() 使用新方法 |
| weixin-java-pay/src/test/java/com/github/binarywang/wxpay/v3/util/RsaCryptoUtilTest.java | 新增测试类,包含嵌套对象、直接字段和继承场景的测试用例 |
weixin-java-pay/src/main/java/com/github/binarywang/wxpay/v3/util/RsaCryptoUtil.java
Outdated
Show resolved
Hide resolved
weixin-java-pay/src/test/java/com/github/binarywang/wxpay/v3/util/RsaCryptoUtilTest.java
Outdated
Show resolved
Hide resolved
weixin-java-pay/src/test/java/com/github/binarywang/wxpay/v3/util/RsaCryptoUtilTest.java
Outdated
Show resolved
Hide resolved
weixin-java-pay/src/test/java/com/github/binarywang/wxpay/v3/util/RsaCryptoUtilTest.java
Outdated
Show resolved
Hide resolved
weixin-java-pay/src/test/java/com/github/binarywang/wxpay/v3/util/RsaCryptoUtilTest.java
Outdated
Show resolved
Hide resolved
weixin-java-pay/src/test/java/com/github/binarywang/wxpay/v3/util/RsaCryptoUtilTest.java
Show resolved
Hide resolved
weixin-java-pay/src/test/java/com/github/binarywang/wxpay/v3/util/RsaCryptoUtilTest.java
Show resolved
Hide resolved
weixin-java-pay/src/test/java/com/github/binarywang/wxpay/v3/util/RsaCryptoUtilTest.java
Outdated
Show resolved
Hide resolved
weixin-java-pay/src/test/java/com/github/binarywang/wxpay/v3/util/RsaCryptoUtilTest.java
Show resolved
Hide resolved
🤖 Augment PR SummarySummary: 本 PR 修复了微信支付 v3 敏感信息加密在“继承字段/嵌套对象”场景下无法生效的问题,确保分账等请求里被 @SpecEncrypt 标注的字段能被正确处理。 Changes:
Technical Notes: 通过在遍历时于 🤖 Was this summary useful? React with 👍 or 👎 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| * 验证 List<Receiver> 中每个 Receiver 对象的 name 字段是否能被正确找到和处理 | ||
| */ | ||
| @Test | ||
| public void testEncryptFieldsWithNestedObjects() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| System.out.println("使用 getDeclaredFields 找到的字段数: " + childFields.length); | ||
|
|
||
| // 使用 getAllFields 辅助方法应该能找到父类和子类的所有字段 | ||
| List<Field> allFields = getAllFields(ChildRequest.class); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
…til/RsaCryptoUtil.java Co-authored-by: Copilot <[email protected]>
…til/RsaCryptoUtilTest.java Co-authored-by: Copilot <[email protected]>
|
@copilot apply changes based on the comments in this thread |
Co-authored-by: binarywang <[email protected]>
已根据 PR Review 反馈完成所有修改:
所有测试通过,Checkstyle 检查通过。提交: bd0ba53 |
|
auggie review |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Review completed. No suggestions at this time.
Comment augment review to trigger a new review at any time.
修复 RSA 加密工具无法加密嵌套对象字段的问题 ✅
问题描述
RsaCryptoUtil.encryptFields只能加密当前类中的@SpecEncrypt字段,无法加密集合中嵌套对象的字段RsaCryptoUtil.encryptField方法以支持继承字段最新改进(基于 PR Review 反馈)
System.out.println调试语句getAllFields()方法,改用反射调用生产代码中的私有方法ChildRequest.canEqual()方法添加@Override注解技术细节与修复
根本原因
原实现使用
Class.getDeclaredFields()方法,该方法只返回当前类中声明的字段,不包括从父类继承的字段。修复方案
新增
getAllFields()辅助方法,递归遍历类的继承链,获取包括父类在内的所有字段。代码修改
性能优化
Collections.addAll()批量添加字段Object.class处停止遍历,避免处理不必要的系统字段测试覆盖
创建了完整的单元测试套件(4个测试用例):
所有测试均通过 ✅
代码质量验证
实际影响
此修复解决了用户报告的核心问题:
ProfitSharingV3Request中receivers集合内每个Receiver对象的name字段现在可以被正确加密@SpecEncrypt字段也能被正确处理向后兼容性
Original prompt
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.